package com.kanche.bigbaby.collection;

import android.content.Context;
import android.widget.Toast;

import com.kanche.bigbaby.R;

import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit.RetrofitError;

//import com.kkche.bss.R;

/**
 * Created by nealmi on 6/27/14.
 */
public final class Guard {
    private Guard() {
    }

    public static void handleError(Context context, RetrofitError error) {
        String msg;
        try {
            if (error.getKind() == RetrofitError.Kind.HTTP) {
                msg = dealBizError(context, error);
                if (msg == null) {
                    msg = context.getResources().getString(R.string.http_error)
                            + "(" + error.getResponse().getReason() + ")";
                }
            } else if (error.getKind() == RetrofitError.Kind.CONVERSION) {
                msg = context.getResources().getString(R.string.data_error_de_serializing)
                        + "(" + error.getResponse().getReason() + ")";
            } else if (error.getKind() == RetrofitError.Kind.NETWORK) {
                msg = dealBizError(context, error);
                if (msg == null) {
                    msg = dealNetworkError(context, error);
                }
            } else {
                msg = "未知错误-" + error.getResponse().getReason();
            }
        } catch (Exception e) {
            msg = "未知错误-" + error.getCause().toString();
        }

        showMessage(context, msg);
    }

    private static String dealNetworkError(Context context, RetrofitError error) {
        String msg = null;
        if (error.getCause() instanceof UnknownHostException) {
            msg = "无法连接到服务器，请检查网络设置";
        } else if (error.getCause() instanceof SocketTimeoutException) {
            msg = "连接服务器超时，请检查网络设置";
        } else if (error.getCause() instanceof SocketException) {
            msg = "无法连接到服务器，请检查网络设置";
        } else {
            msg = "未知错误-" + error.getResponse().getReason() + "(" + error.getResponse().getReason() + ")";
        }
        return msg;
    }

    private static String dealBizError(Context context, RetrofitError error) {
        String msg = null;
//        try {
//            ApiResult result = (ApiResult) error.getBodyAs(ApiResult.class);
//
//            msg = result.getMessage();
//            int resultCode = result.getCode();
//            if (resultCode == 4003) {
//                msg = context.getResources().getString(R.string.biz_error_phone_unregistered)
//                        + "(" + resultCode + ")";
//            } else if (resultCode == 4006) {
//                msg = context.getResources().getString(R.string.biz_error_phone_exist)
//                        + "(" + resultCode + ")";
//            } else if (resultCode == 4001) {
//                msg = context.getResources().getString(R.string.biz_error_wrong_username_passowrd)
//                        + "(" + resultCode + ")";
//            } else if (resultCode == 4101) {
//                msg = context.getResources().getString(R.string.biz_error_wrong_bind_username_passowrd)
//                        + "(" + resultCode + ")";
//            } else if (resultCode == 1000) {
//                msg = context.getResources().getString(R.string.biz_error_invalid_intviter_phone)
//                        + "(" + resultCode + ")";
//
//            } else if (resultCode == 6002) {
//                msg = context.getResources().getString(R.string.biz_error_fav_exist)
//                        + "(" + resultCode + ")";
//
//            } else {
//                msg = context.getResources().getString(R.string.error_system)
//                        + "(" + resultCode + ")";
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
        return msg;


    }

    public static void showMessage(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    public static void showMessage(Context context, int messageId) {
        Toast.makeText(context, messageId, Toast.LENGTH_LONG).show();
    }
}
